//
//  CreateJobBaseView.swift
//  GreenDeal
//
//  Created by Moennig Q on 1/3/2023.
//

import UIKit
import SnapKit
import IQKeyboardManagerSwift
import RxSwift
import RxCocoa
class CreateJobBaseView: GreenNaviBarView {
    lazy var disposeBag = DisposeBag()
    @objc dynamic var createJobReviewEditFlag: Bool = false//是否是创建job，review的编辑功能，是就隐藏返回箭头
    var continueClosure: (()->Void)?
    let continueBtn = OperationButton(R.string.localizable.continue())
    lazy var scrollView = DIYScrollView()
    let contentView = IQPreviousNextView()
    var useScrollViewFlag: Bool = true
    lazy var cancelBtn = BaseButton()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    init(topBGImageName: String = "", useScrollViewFlag: Bool = true) {
        self.useScrollViewFlag = useScrollViewFlag
        super.init(topBGImageName: topBGImageName, customBackToPreFlag: true)
    }
    override func setUpView() {
        //增加右上角的cancel btn
        self.addRightCancelBtn()
        //最下边的continue  btn
        self.addSubview(continueBtn)
        continueBtn.snp.makeConstraints { make in
            make.bottom.equalTo(self.safeAreaLayoutGuide.snp.bottom).offset(Constant.BottomMargin)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(OperationButton.height)
        }
        //
        if self.useScrollViewFlag {//内容用到滚动视图
            scrollView.drawCorner(byRoundingCorners: [.topLeft,.topRight])
            scrollView.backgroundColor = .white
            self.addSubview(scrollView)
            scrollView.snp.makeConstraints { (make) in
                make.top.equalTo(self.topBackView.snp.bottom).offset(-20)
                make.left.right.equalTo(self)
                make.bottom.equalTo(continueBtn.snp.top)
            }
            
            contentView.backgroundColor = .white
            scrollView.addSubview(contentView)
            
            contentView.snp.makeConstraints { (make) in
                make.top.bottom.equalTo(scrollView)
                make.left.right.equalTo(self)
                make.height.greaterThanOrEqualTo(0)
            }
        }else{
            contentView.drawCorner(byRoundingCorners: [.topLeft,.topRight])
            contentView.backgroundColor = .white
            self.addSubview(contentView)
            contentView.snp.makeConstraints { (make) in
                make.top.equalTo(self.topBackView.snp.bottom).offset(-20)
                make.left.right.equalTo(self)
                make.bottom.equalTo(continueBtn.snp.top)
            }
        }
        continueBtn.addTarget(self, action: #selector(continueBtnClick), for: UIControl.Event.touchUpInside)
        self.rx.observe(Bool.self, "createJobReviewEditFlag").subscribe(onNext: { [weak self]  flag in
            let realFlag = flag ??  false
            self?.backToPreBtn.isHidden = realFlag
        }).disposed(by: self.disposeBag)
        self.setUpContentView()
    }
    func setUpContentView(){
        
    }
    //MARK: - 继续下一步
    @objc private func continueBtnClick(){
        self.endEditing(true)
        self.continueClosure?()
    }
    //MARK: 增加右边的cancel btn，取消创建Job，退回到rootVC
    private func addRightCancelBtn(){
        //cancel btn
        cancelBtn.setTitle(R.string.localizable.cancel(), for: UIControl.State.normal)
        cancelBtn.setTitleColor(UIColor.white, for: UIControl.State.normal)
        cancelBtn.setFont(R.font.interMedium(size: 15))
        self.topBackView.addSubview(cancelBtn)
        cancelBtn.snp.makeConstraints { make in
            make.left.equalTo(titleBackView.snp.right).offset(5)
            make.right.equalTo(-16)
            make.height.equalTo(40)
            make.width.equalTo(60 * Constant.currentSizeScale)
            make.centerY.equalTo(backToPreBtn)
        }
        cancelBtn.addTarget(self, action: #selector(cancelBtnClick), for: UIControl.Event.touchUpInside)
    }
    @objc func cancelBtnClick(){
        Tool().showAlertViewWithTwoBtns(title: "Cancel Form Creation",message: "Are you sure you want to cancel?", yesClosure:  {
            if let currentNavi = self.firstNaviViewController(){
                currentNavi.popToRootViewController(animated: false)
            }
        })
    }
}
